package com.cobo.zettelkasten.utils
import java.text.MessageFormat
import java.text.SimpleDateFormat
import java.util.*

/**
 * 简单的日志打印类
 * */
class Logger {

    enum class Level {
        Err,
        Debug,
    }

    companion object {
        val sdf: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.s")

        val callerClass: StackTraceElement?
            get() {
                val stackTrace = Throwable().stackTrace
                for (element in stackTrace) {
                    if (!element.className.startsWith("com.cobo.zettelkasten.utils")) {
                        return element
                    }
                }
                return null
            }

        fun formatMsg(level: Level, msg: String?): String {
            val now = Date()
            val callerClass = callerClass
            return MessageFormat.format(
                "[{0}] {1} {3}::{4}:{5} {2} ", level.name, sdf.format(now), msg, callerClass!!.fileName,
                callerClass.methodName, callerClass.lineNumber
            )
        }

        fun error(msg: String?) {
            System.err.println(formatMsg(Level.Err, msg))
        }

        fun debug(msg: String?) {
            println(formatMsg(Level.Debug, msg))
        }
    }


}